/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.svm.funds.service.plugins  
 */
package com.apache.funds.service.plugins;

import java.lang.reflect.Field;
import java.util.List;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.funds.common.FundsUtil;
import com.apache.funds.manager.SvmCaptionBalanceManager;
import com.apache.funds.util.Constants;
import com.apache.funds.util.FundsTemplate;
import com.apache.funds.vo.UserFundsVo;
import com.apache.tools.StrUtil;

/**
 * 本类主要涉及业务逻辑<br>
 * 1.所有资金余额判断,主要是在out操作的判断相应科目的可余额；
 * @author Administrator
 *
 */
public class FundsLimitPlugin implements PluginConnector {

	private SvmCaptionBalanceManager svmCaptionBalanceManager;

	public void setSvmCaptionBalanceManager(SvmCaptionBalanceManager svmCaptionBalanceManager) {
		this.svmCaptionBalanceManager = svmCaptionBalanceManager;
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)  
	 */
	@Override
	public Object execute(ParamsVo vo) throws Exception {
		ResultEntity re = new ResultEntity();
		re.setResult("true");
		re.setEntity("false");
		re.setMessage(Constants.Msg.F008);

		//add hexin 2015-12-08 余额不再实时查询库，改用缓存
		String userId = String.valueOf(vo.getParams("userId"));
		String tradeType = String.valueOf(vo.getParams("tradeType"));
		String operAmt = String.valueOf(vo.getParams("amount"));
		if (StrUtil.isNull(userId) || StrUtil.isNull(tradeType) || StrUtil.isNull(operAmt)) {
			re.setMessage(Constants.Msg.F006);
			re.setEntity("false");
			return re;
		}
		UserFundsVo uf = FundsUtil.selectFundsMyMoneyByCache(userId);
		if (uf == null) {
			re.setEntity("false");
			re.setMessage(Constants.Msg.F010 + "（" + userId + "）");
			return re;
		}
		//转换金额
		long amount = Long.valueOf(operAmt);
		if (amount == 0 || amount < 0) {
			re.setMessage(Constants.Msg.F009 + "，本次操作金额[" + amount + "]，不需要校验！");
			re.setEntity("true");
			return re;
		}
		re.setMessage(Constants.Msg.F009);
		re.setEntity("true");

		List<FundsTemplate> templates = Constants.getStorageTemplate(tradeType).get(tradeType);
		// 利用反射机制，判断每一个交易码里所有为out的金额是否小于当前amount的数量
		Field[] fields = uf.getClass().getDeclaredFields();
		for (FundsTemplate st : templates) {
			//			System.out.println(st.getDirection() + " 对应 "+ st.getCaption());
			if (st.getDirection().equals(Constants.Description.OUT)) {
				for (Field field : fields) {
					//					System.out.println(field + " - "+ field.getName());
					if (st.getCaption().equals(field.getName())) {
						field.setAccessible(true);
						long val = Long.valueOf(String.valueOf(field.get(uf)));
						if (val < amount) {
							re.setMessage(Constants.Msg.F005 + "（" + userId + "的" + st.getCaption() + "余额不足）");
							re.setEntity("false");
							return re;
						}
					}
				}
			}
		}
		return re;
	}
}
